package util.validate
{
	import MyApp.Model.Business.Staff;
	
	import mx.collections.ArrayCollection;
	import mx.utils.StringUtil;
	import mx.validators.ValidationResult;
	import mx.validators.Validator;
	
	import nz.co.codec.flexorm.EntityManager;
	import nz.co.codec.flexorm.criteria.Criteria;
	
	import util.mycomponents.MyHex;
	
	public class LoginPasswordValidator extends Validator
	{
		
		private var _passwordError:String;
		[Bindable]
		private var _staff:Object;
		
		public function LoginPasswordValidator()
		{
			super();
		}
		public function set staff(value:Object):void{
			this._staff=value;
		}
		[Bindable]
		public function get staff():Object{
			return this._staff;
		}
		public function set passwordError(value:String):void{
			this._passwordError=value;
		}
		public function get passwordError():String{
			return this._passwordError;
		}
		override protected function doValidation(value:Object):Array{
			var results:Array=super.doValidation(value);
			if(results.length>0){
				return results;
			}
			if(staff==null){
				results.push(new ValidationResult(true,"","userError","请还没有将用户名填写完整"));
				return results;
			}
			var password:String=String(value);
			password=StringUtil.trim(password);
			password=MyHex.fromString(password);//变成加密字符串去匹配
			//数据库验证
			var em:EntityManager=EntityManager.getInstance();
			var cri:Criteria=em.createCriteria(Staff);
			cri.addEqualsCondition("staff_id",(staff as Staff).staff_id);
			cri.addEqualsCondition("password",password);
			var data:ArrayCollection=em.fetchCriteria(cri);
			if(data.length==0){
				results.push(new ValidationResult(true,"","passwordError",passwordError));
			}else{
				staff=data.getItemAt(0);
			}
			return results;
		}
	}
}